package GUI;
import CTRLayer.*;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.ListSelectionModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JScrollPane;

public class Customers extends JFrame {

	private JPanel contentPane;
	private JTextField txtID;
	private JTextField txtFName;
	private JTextField txtLName;
	private JTextField txtPhone;
	private JTextField txtAddress;
	private JTextField txtCity;
	private JTextField txtZIP;
	private JTable tableCustomers;
	private JButton btnAddNew;
	private JButton btnShowInformation;
	private JButton btnDelete;
	private JButton btnBackToMain;
	private AddCustomer addcustomer;
	private Controller ctr;
	
	/**
	 * Create the frame.
	 */
	public Customers() {
		
		ctr = new Controller();
		
		setResizable(false);
		setTitle("Customers");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 455, 450);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblFirstName = new JLabel("First name:");
		lblFirstName.setBounds(10, 36, 70, 14);
		contentPane.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last name:");
		lblLastName.setBounds(10, 61, 70, 14);
		contentPane.add(lblLastName);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(262, 11, 70, 14);
		contentPane.add(lblAddress);
		
		JLabel lblCity = new JLabel("City:");
		lblCity.setBounds(262, 36, 70, 14);
		contentPane.add(lblCity);
		
		JLabel lblZipCode = new JLabel("ZIP code:");
		lblZipCode.setBounds(262, 61, 70, 14);
		contentPane.add(lblZipCode);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(10, 86, 70, 14);
		contentPane.add(lblPhone);
		
		JLabel lblId = new JLabel("ID:");
		lblId.setBounds(10, 11, 70, 14);
		contentPane.add(lblId);
		
		txtID = new JTextField();
		txtID.setBounds(79, 8, 86, 20);
		contentPane.add(txtID);
		txtID.setColumns(10);
		
		txtFName = new JTextField();
		txtFName.setBounds(79, 33, 86, 20);
		contentPane.add(txtFName);
		txtFName.setColumns(10);
		
		txtLName = new JTextField();
		txtLName.setBounds(79, 58, 86, 20);
		contentPane.add(txtLName);
		txtLName.setColumns(10);
		
		txtPhone = new JTextField();
		txtPhone.setBounds(79, 83, 86, 20);
		contentPane.add(txtPhone);
		txtPhone.setColumns(10);
		
		txtAddress = new JTextField();
		txtAddress.setBounds(342, 8, 86, 20);
		contentPane.add(txtAddress);
		txtAddress.setColumns(10);
		
		txtCity = new JTextField();
		txtCity.setBounds(342, 33, 86, 20);
		contentPane.add(txtCity);
		txtCity.setColumns(10);
		
		txtZIP = new JTextField();
		txtZIP.setBounds(342, 58, 86, 20);
		contentPane.add(txtZIP);
		txtZIP.setColumns(10);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.setBounds(340, 82, 90, 25);
		contentPane.add(btnSearch);
		
		
 		tableCustomers = new JTable();
		tableCustomers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		tableCustomers.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"ID", "First name", "Last name", "Phone", "Address", "City", "ZIP Code"
			}
		) {
			Class[] columnTypes = new Class[] {
				String.class, String.class, String.class, String.class, String.class, String.class, String.class
			};
			public Class getColumnClass(int columnIndex) {
				return columnTypes[columnIndex];
			}
		});
		tableCustomers.getColumnModel().getColumn(0).setResizable(false);
		tableCustomers.getColumnModel().getColumn(1).setResizable(false);
		tableCustomers.getColumnModel().getColumn(2).setResizable(false);
		tableCustomers.getColumnModel().getColumn(3).setResizable(false);
		tableCustomers.getColumnModel().getColumn(4).setResizable(false);
		tableCustomers.getColumnModel().getColumn(5).setResizable(false);
		tableCustomers.getColumnModel().getColumn(6).setResizable(false);
		tableCustomers.setVisible(true);
		
		JScrollPane scrollPane = new JScrollPane(tableCustomers);
		scrollPane.setBounds(10, 111, 419, 227);
		contentPane.add(scrollPane);
		

		
		
		btnAddNew = new JButton("Add new");
		btnAddNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addcustomer=new AddCustomer();
				addcustomer.setVisible(true);
			}
		});
		btnAddNew.setBounds(10, 350, 115, 23);
		contentPane.add(btnAddNew);
		
		btnShowInformation = new JButton("Show information");
		btnShowInformation.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int SelRowIndex = tableCustomers.getSelectedRow();
				if(SelRowIndex==-1)
				{
					JOptionPane.showMessageDialog(null,
					    "You have to chose one from the list",
					    "Warning",
					    JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					String idSelected = (String) tableCustomers.getValueAt(SelRowIndex, 0);
					JOptionPane.showMessageDialog(null,
					    ctr.custInfo(idSelected));
					    /*data about customer selected*/
				}
			}
		});
		
		btnShowInformation.setBounds(160, 350, 115, 23);
		contentPane.add(btnShowInformation);
		
		btnDelete = new JButton("Delete");
		btnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				int SelRowIndex = tableCustomers.getSelectedRow();
				if(SelRowIndex==-1)
				{
					JOptionPane.showMessageDialog(null,
					    "You have to chose one from the list",
					    "Warning",
					    JOptionPane.WARNING_MESSAGE);
				}
				else
				{
					String idSelected = (String) tableCustomers.getValueAt(SelRowIndex, 0);				
					if(ctr.deleteCustomer(idSelected))
					{
						JOptionPane.showMessageDialog(null,
								"Deleted!"); 
					}
				}
			}
		});
		btnDelete.setBounds(313, 350, 115, 23);
		contentPane.add(btnDelete);
		
		btnBackToMain = new JButton("Back to Main Menu");
		btnBackToMain.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				MainMenu.getInstance().setVisible(true);
				dispose();
			}
		});
		btnBackToMain.setBounds(147, 388, 144, 23);
		contentPane.add(btnBackToMain);
		
		
		
	
	}
}
